# @Time : 2020/7/29 11:07
# @Author : Fioman 
# @Phone : 13149920693
import cv2 as cv
import numpy as np

"""
contourArea()计算轮廓面积,将各个轮廓显示出来,还有轮廓的面积.
"""
img = cv.imread("moments.jpg", cv.IMREAD_COLOR)[2:,:-3]
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

T, thresOtsu = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow("ThresOtsu", thresOtsu)

image, contours, hierarchy = cv.findContours(thresOtsu, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)

for index, contour in enumerate(contours):
    temp = np.zeros(img.shape,np.uint8)
    contourImg = temp
    cv.drawContours(contourImg,contours,index,(255,255,255),2)
    cv.imshow("contours[{}]".format(index),contourImg)
    print("contours {} 的面积 = {}".format(index,cv.contourArea(contour)))

cv.waitKey(0)
cv.destroyAllWindows()